import { useData } from "@sker/runtime"
import { toSuccess, useEntityManager } from "@sker/core"
import { z } from "zod"
import { KsAccount } from "@sker/entities"

export default async () => {
    const input = z.object({
        fans_count: z.optional(z.number()),
        follow_count: z.optional(z.number()),
        like_count: z.optional(z.number()),
        user_name: z.optional(z.string()),
        ksid: z.optional(z.string()),
        desc: z.optional(z.string()),
        userId: z.string(),
        has_live: z.boolean()
    })
    const body = input.parse(useData())
    body.follow_count = body.follow_count || 0;
    body.like_count = body.like_count || 0;
    body.fans_count = body.fans_count || 0;
    return await useEntityManager(async m => {
        const one = await m.findOne(KsAccount, { where: { user_id: body.userId } })
        if (!one) throw new Error(`参数错误`)
        const data = m.create(KsAccount, {
            id: one?.id,
            fans_count: body.fans_count,
            status: '1',
            has_live: body.has_live
        });
        await m.save(KsAccount, data)
        return toSuccess(true)
    })
}